package com.wufeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.constant.CachePrefix;
import com.wufeng.dto.R;
import com.wufeng.dto.UserDto;
import com.wufeng.entity.User;
import com.wufeng.service.UserService;
import com.wufeng.service.cache.CacheService;
import com.wufeng.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


/**
 * 验证码控制
 */
@RestController
@RequestMapping("/verificationcode")
public class VerificationCodeController {


    @Autowired
    private CacheService cacheService;

    @Autowired
    private UserService userService;


    /**
     * 忘记密码发送验证码，验证验证码的有效性
     *
     * @param userDto
     * @return
     */
    @PostMapping("/checkCode")
    public R checkCode(@RequestBody UserDto userDto) {

        String key = CachePrefix.FORGOT.getPrefix() + userDto.getPhonenumber();
        // 校验验证码
        String code = cacheService.get(key);

        if (code == null) {
            return R.error("验证码错误");
        }

        List<User> list = userService.list(new QueryWrapper<User>().eq("phoneNumber", userDto.getPhonenumber()));
        if (list.isEmpty()) {
            return R.error("该手机号未绑定任何账户");
        }

        code = StringUtils.removeQuotes(code);
        System.out.println(code);
        if (userDto == null || !code.equals(userDto.getVerificationCode())) {
            return R.error("验证码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("pass", true);

        return R.ok(map);
    }

    /**
     * 根据手机号，以及前缀验证code，
     * 前缀用来确定取redis中的数据得key的形式
     *
     * @param userDto
     * @return
     */
    @PostMapping("/checkCodeWithPrefix")
    public R checkCodeWithPrefix(@RequestBody UserDto userDto) {

        String key = userDto.getPrefix() + userDto.getPhonenumber();
        // 校验验证码
        String code = cacheService.get(key);

        if (code == null) {
            return R.error("验证码错误");
        }

        //如过是绑定则判断
        List<User> list = userService.list(new QueryWrapper<User>().eq("phoneNumber", userDto.getPhonenumber()));
        if (userDto.getPrefix().equals(CachePrefix.BINDING.getPrefix()) && !list.isEmpty()) {
            return R.error("该手机号已绑定某个用户，不可再次绑定");
        }

        code = StringUtils.removeQuotes(code);
        System.out.println(code);
        if (userDto == null || !code.equals(userDto.getVerificationCode())) {
            return R.error("验证码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("pass", true);
        return R.ok(map);
    }

    /**
     * 根据用户id检查验证码合法性
     * @param userId
     * @param prefix
     * @return
     */
    @GetMapping("/checkCodeByUserIdWithPrefix")
    public R checkCodeByUserIdWithPrefix(@RequestParam("userId")Integer userId ,
                                         @RequestParam("prefix")String prefix,
                                         @RequestParam("verificationCode") String verificationCode) {

        User user = userService.getById(userId);
        if(user==null||user.getPhonenumber()==null||user.getPhonenumber().length()!=11){
            return R.error("请求参数有误");
        }
        String key = prefix+user.getPhonenumber();
        String code = cacheService.get(key);

        if(code!=null){
            code = StringUtils.removeQuotes(code);
        }
        if(code==null||!code.equals(verificationCode)){
            return R.error("验证码错误");
        }

        return R.ok();
    }

}
